const util = require('../../../utils/util.js');
const api = require('../../../config/api.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: 0,
    page: 1,
    limit: 10,
    totalPages: 1,
    endFlag: false,
    records: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (param) {
    var type = param.type != null ? param.type : 0;
    this.setData({
      type: type
    });
    this.loadData();
  },
  loadData: function () {
    var hasLogin = wx.getStorageSync("app.globalData.hasLogin");
    if (!hasLogin) {
      wx.navigateTo({
        url: "/pages/auth/login/login"
      });
    }

    var that = this;
    var userid = wx.getStorageSync("userid");
    if(userid == ''){
      wx.navigateTo({
        url: "/pages/auth/login/login"
      });
      return;
    }
    util.request(api.AppointmentRecord, {
      type: that.data.type,
      page: that.data.page,
      limit: that.data.limit,
      uid:userid,
      key:api.key
    })
      .then(function (res) {
        if (res.errno == 0) {
          that.setData({
            records: res.data.list,
            totalPages: res.data.pages
          });
        }else if(res.errno === 402){
          wx.showModal({
            title: '错误',
            content:'不被允许的请求',
            showCancel: false
          })
      } else {
          util.showErrorToast(res.errmsg);
        }
      });
  },
  tabClick: function (e) {
    this.setData({
      records: [],
      type: e.currentTarget.id,
      page: 1,
      limit: 10,
      totalPages: 1
    });
    this.loadData();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    /*if (this.data.totalPages > this.data.page) {
      this.setData({
        page: this.data.page + 1
      });
      this.getSchools();
    } else {
      this.setData({
        endFlag: true
      });
      return false;
    }*/
  },
  toEval: function (e) {
    var param = e.currentTarget.dataset;
    var id = param["id"];
    var coachName = param["coachname"];
    wx.navigateTo({
      url: '/pages/eval/coach/add?id=' + id + '&coachName=' + coachName,
    })
  }
})